﻿using AntMovement.Core.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.PCI1230.Domain
{
    public class HardwareUpdateDI : AntMovement.Core.IHardwareUpdate
    {
        private List<DI> _values = new List<DI>();
        private int _nCardNo;

        public HardwareUpdateDI(int nCardNo, DI[] values)
        {
            _nCardNo = nCardNo;
            _values.AddRange(values);
        }

        public string Update()
        {
            uint data = 0;
            long error = CPcie1230.Pci1230Read(_nCardNo, ref data);
            if (error != 1024) { return $"Pci1230Read ==>> {(PCI1230Error)error}"; }
            int i = 0;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 1;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 2;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 3;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 4;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 5;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 6;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 7;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 8;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 9;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 10;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 11;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 12;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 13;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 14;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            i = 15;
            _values[i].Status = ((data >> i) & 0x01) == 1;
            if (_values.Count > 16)
            {
                i = 16;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 17;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 18;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 19;
                _values[i].Status = ((data >> i) & 0x01) == 1; 
                i = 20;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 21;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 22;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 23;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 24;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 25;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 26;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 27;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 28;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 29;
                _values[i].Status = ((data >> i) & 0x01) == 1; 
                i = 30;
                _values[i].Status = ((data >> i) & 0x01) == 1;
                i = 31;
                _values[i].Status = ((data >> i) & 0x01) == 1;
            }
            return "ok";
        }
    }
}
